package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.merchant.GroupEntity;
import com.dragon.modules.yuegou.entity.merchant.GroupUseRefEntity;
import com.dragon.modules.yuegou.enums.code.MerchantCode;
import com.dragon.modules.yuegou.mapper.merchant.GroupMapper;
import com.dragon.modules.yuegou.model.merchant.Group;
import com.dragon.modules.yuegou.query.merchant.GroupQuery;
import com.dragon.modules.yuegou.query.merchant.GroupUseRefQuery;
import com.dragon.modules.yuegou.service.merchant.GroupService;
import com.dragon.modules.yuegou.service.merchant.GroupUseRefService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户分组表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-26 11:02:04
 * @version 2.0
 */
@Slf4j
@Service("merGroupService")
public class GroupServiceImpl
		extends ServiceImpl<GroupMapper, GroupEntity>
		implements GroupService {

	@Autowired
	private GroupUseRefService groupUseRefService;

	private static final String BUSD_ID_IS_EMPTY = "商圈id不能为空";

	@Override
	public IPage<Group> search(PageRequest page, GroupQuery query){
		Check.notNull(query.getBusdId(),BUSD_ID_IS_EMPTY);

		QueryWrapper<GroupEntity> queryWrapper = query.toQueryWrapper();
		queryWrapper.orderByDesc(GroupEntity.COL_CREATED_TIME);
		IPage<GroupEntity> iPage = page(PageToResultUtils.resultToPage(page),queryWrapper);
		return iPage.convert(Group::new);
	}

	@Override
	public void saveGroup(Group group){
		Check.notNull(group.getBusdId(),BUSD_ID_IS_EMPTY);
		Check.notNull(group.getGroupName(),"分组名称不能为空");
		log.info("保存商户分组，开始==，分组名称={}",group.getGroupName());
		GroupQuery groupQuery = new GroupQuery();
		groupQuery.setBusdId(group.getBusdId());
		groupQuery.setGroupName(group.getGroupName());
		if (!StringUtils.isEmpty(group.getId())) {
			List<GroupEntity> list = list(groupQuery.toQueryWrapper());
			if (!CollectionUtils.isEmpty(list) && !list.get(0).getId().equals(group.getId())) {
				log.info("修改商户分组，分组名称已存在，分组名称={}",group.getGroupName());
				throw new DragonException(MerchantCode.MERCHANT_GROUP_NAME_EXISTS);
			}
			//修改分组
			updateById(group);
			log.info("保存商户分组，修改分组成功==，分组名称={}",group.getGroupName());
		}else {
			Long count = count(groupQuery.toQueryWrapper());
			if (count>0) {
				log.info("新增商户分组，分组名称已存在，分组名称={}",group.getGroupName());
				throw new DragonException(MerchantCode.MERCHANT_GROUP_NAME_EXISTS);
			}
			//新增分组
			group.setDel(false);
			save(group);
			log.info("保存商户分组，新增分组成功，分组名称={}",group.getGroupName());
		}
	}

	@Transactional
	@Override
	public void delGroup(String id){
		Check.notNull(id,"分组id不能为空");
		log.info("删除商户分组，开始==，分组id={}",id);
		//逻辑删除分组
		removeById(id);
		log.info("删除商户分组，删除成功，分组id={}",id);
	}

	@Override
	public List<Group> getMerchantGroup(String busdId, String userId){
		Check.notNull(busdId,BUSD_ID_IS_EMPTY);
		Check.notNull(userId,"用户id不能为空");
		GroupQuery query = new GroupQuery();
		query.setBusdId(busdId);
		List<GroupEntity> groupEntities = list(query.toQueryWrapper());
		if (CollectionUtils.isEmpty(groupEntities)){
			return new ArrayList<>();
		}
		List<Group> groupList = groupEntities.stream().map(Group::new).collect(Collectors.toList());
		groupList.stream().forEach(group -> {
			group.setSelected(false);
			GroupUseRefQuery groupUseRefQuery = new GroupUseRefQuery();
			groupUseRefQuery.setDel(false);
			groupUseRefQuery.setUserId(userId);
			groupUseRefQuery.setGroupId(group.getId());
			List<GroupUseRefEntity> list = groupUseRefService.list(groupUseRefQuery.toQueryWrapper());
			if (!CollectionUtils.isEmpty(list)){
				group.setSelected(true);
			}
		});
		return groupList;
	}

}